<?php

namespace app\admin\controller;

use app\admin\model\Menu;
use app\admin\model\Substation;

/**
 * 欢迎模型控制器
 * Class Home
 * @package app\admin\controller
 */
class Home extends Base
{

    //首页
    public function index(){
        $Menu = new Menu();
        $userMenuList = $Menu->userMenuList();
        $Substation = new Substation();
        $userListCount = $Substation->userListCount($this->admin["group_id"]);
        return view('index',[
            'title'             =>  '别墅工场网站管理系统',
            'userMenuList'      =>  $userMenuList,
            'userListCount'     =>  $userListCount,
        ]);
    }

    //设置当前管理网站
    public function setSubstation(){
        $Substation = new Substation();
        if(request()->isPost()){
            $substation_id = input("substation_id/d",0);
            if($Substation->checkUserSubstation($substation_id)){//验证是否有权限设置
                $this->substations = $substation_id;
                session("substations",$this->substations);//设置当前网站
                $result["code"] = 0;
                $result["msg"] = "设置成功!";
            }else{
                $result["code"] = 1;
                $result["msg"] = "没有权限管理该网站!";
            }
        }else {
            //获取网站
            $userList = $Substation->userList();
            $userListCount = $Substation->userListCount($this->admin["group_id"]);
            $this->assign('userList', $userList);
            $this->assign('userListCount', $userListCount);
        }
        return !empty($result) ? json($result) : view('setSubstation',[
            'title'             =>  '设置当前管理网站',
            'substations'       =>  $this->substations,//设置单前管理网站
        ]);
    }
}
